#include "WndReg.h"
#include <assert.h>

CWndReg::CWndReg(UINT size)
{
    m_uSize = size;
    m_uCount = 0;
    m_pArray = (HWND*) malloc (size * sizeof(HWND));
    memset (m_pArray, 0, size * sizeof(HWND));
}

CWndReg::~CWndReg()
{
    if (m_pArray) {
        free (m_pArray);
    }
}

int CWndReg::AddWnd(HWND hWnd)
{
    assert (m_uCount <= m_uSize);
    assert (m_pArray);
    if (m_uCount == m_uSize) {
        return -1;
    }
    m_pArray[m_uCount] = hWnd;
    m_uCount++;
    return 0;
}

void CWndReg::RemoveWnd(HWND hWnd)
{
    UINT i;
    assert (m_uCount <= m_uSize);
    assert (m_pArray);

    for (i = 0; i < m_uCount; i++) {
        if (m_pArray[i] == hWnd) {
            break;
        }
    }
    if (i >= m_uCount) {
        /* Not registered. */
        return;
    }
    assert (m_uCount > 0 && i < m_uCount);
    if (i +1 < m_uCount) {
        memmove((m_pArray + i), (m_pArray +i + 1), (m_uCount - i-1)*sizeof(HWND));
    }
    m_uCount--;
    return;
}


